<template>
  <div>
    <el-card>
      <el-row>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">数据中心</el-breadcrumb-item>
          <el-breadcrumb-item> 车位管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>

      <!--1.多条件查询栏 -->
      <el-row>
        <el-col>
          <el-form
            :inline="true"
            :model="stall"
            class="demo-form-inline"
            style="margin-top: 10px"
          >
            <el-form-item label="車位編號">
              <el-input
                v-model="stall.parkingnum"
                placeholder="車位編號"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="車位狀態"
              style="margin-left: 30px; margin-right: 40px"
            >
              <!-- <el-radio-group v-model="stall.status">
                <el-radio   label="未租"></el-radio>
                <el-radio   label="已租"></el-radio>
              </el-radio-group> -->
              <el-select v-model="stall.status" placeholder="--请选择--">
            <el-option label="--请选择--" value=""></el-option>
            <el-option label="未租" value="0"></el-option>
            <el-option label="已租" value="1"></el-option>
          </el-select>
            </el-form-item>

            <el-form-item label="所属小区">
              <el-select
                clearable
                v-model="stall.cell.cid"
                placeholder="请选择部门"
              >
                <el-option
                  v-for="item of cellList"
                  :key="item.cid"
                  :label="item.cname"
                  :value="item.cid"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button
                icon="el-icon-search"
                type="primary"
                circle
                @click="getByCondition()"
              ></el-button>
              <el-button type="primary" @click="clearSearchForm()"
                >清空</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>

      <el-divider>
      </el-divider>
      <!--2.新增、批量删除按钮 -->
      <el-row style="margin-top: 40px; margin-bottom: 10px">
        <el-col :span="6" :offset="0">
          <el-button type="primary" @click="addClick()" size="mini">添加</el-button>
          <el-button type="danger" @click="batchDeleteClick()" size="mini"
            >批量删除</el-button>
        </el-col>
      </el-row>
      <!--3.用户表单 -->
      <el-row>
        <el-col>
          <el-table
            :data="pageBean.beanList"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column label="车位编号" width="70">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.parkingnum }}</span>
              </template>
            </el-table-column>

            <el-table-column label="类别" width="100">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.kind}}</span>
              </template>
            </el-table-column>
            <el-table-column label="所在小区" width="150" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.cell.cname
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="车位状态" width="100" align="center">
              <template slot-scope="scope">
                <div
                  style="margin-left: 10px"
                  :class="statusClass(scope.row.status)"
                >
                  {{ getStatus(scope.row.status) }}
                </div>
              </template>
            </el-table-column>

            <el-table-column label="车位面积" width="200" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.area
                }}</span>
              </template>
            </el-table-column>
             <el-table-column label="车位价格" width="200" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.price
                }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  round
                  plain
                  size="mini"
                  type="primary"
                  @click="handleEdit(scope.$index, scope.row)"
                  >修改</el-button
                >
                <el-button
                  plain
                  round
                  size="mini"
                  type="info"
                  v-if="scope.row.status == 0"
                  @click="setStatus(scope.$index, scope.row)"
                  >未租</el-button
                >
                <el-button
                  plain
                  round
                  size="mini"
                  type="warning"
                  v-if="scope.row.status == 1"
                  @click="setStatus(scope.$index, scope.row)"
                  >已租</el-button
                >
                <el-button
                  plain
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!--4.分页 -->
      <el-row style="margin-top: 40px">
        <el-col>
         <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageIndex"
          :page-sizes="[2,3, 4, 5]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageBean.totalRecord"
        >
        </el-pagination>
        </el-col>
      </el-row>

      <!-- 6.添加修改对话框-->
   
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //1.查询
      pageBean: {},
      cellList: [],
      //2.表格
      multipleSelection: [],
      stall:{cell:{cid:null,cname:''}},
      //3.分页
      pageIndex: 1,
      pageSize: 3,
    
      //5.添加修改
      formLabelWidth: "120px",
      dialogTitle: "",
      dialogFormVisible: false,
      isDisabled: false,
    
    };
  },
  methods: {
    closeDialog() {
      this.carForm = {};
    },
    close() {
      this.dialogVisible = false;
    },
    //多选按钮事件改变时调用
    handleSelectionChange(row) {
      this.multipleSelection = row;
    },
    handleSizeChange(size) {
      //console.log(`每页 ${val} 条`);
      this.pageSize = size;
      this.getByCondition();
    },
    handleCurrentChange(page) {
      //console.log(`当前页: ${page}`);
      this.pageIndex = page;
      this.getByCondition();
    },
    getCellList(){
      this.$axios.get("/cell/getAll").then(res=>{
        if(res.data.code==200){
          this.cellList=res.data.data
        }
      })
    },
    //多条件查询
    getByCondition() {
      console.log("多条件",this.stall);
      this.$axios
        .post(
          "/stall/getAllPg/" + this.pageIndex + "/" + this.pageSize,this.stall
        )
        .then((res) => {
          
          //console.log(res);
          if(res.data.code=200){
            this.pageBean=res.data.data
          }
        });
    },
    //清空多条件查询表单
    clearSearchForm() {
      this.stall = {cell:{cid:null}};
      this.getByCondition();
    },
    // -----------------------表格
    //添加按钮
    addClick() {
      this.dialogFormVisible = true;
    },
    //批量删除按钮
    batchDeleteClick() {
      if (this.multipleSelection.length == 0) {
        this.$message.error("请至少选择一条数据！");
      } else {
        this.$confirm("确定删除这些数据吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let deleteParam = "?";
            for (var index in this.multipleSelection) {
              deleteParam += "uids=" + this.multipleSelection[index].uid + "&";
            }

            this.$axios
              .delete("/user/batchedDelete" + deleteParam)
              .then((respData) => {
                if (respData.data.data) {
                  this.$message.success("批量删除成功！");
                  this.getByCondition();
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    //表格多选框事件
    handleSelectionChange(rows) {
      this.multipleSelection = rows;
      console.log(this.multipleSelection);
    },
    //表格编辑事件
    handleEdit(index, row) {
      this.dialogTitle = "修改用户";
      this.userModify = row;
      this.userModify.did = row.department.did;
      this.isDisabled = true;
      this.dialogFormVisible = true;
    },
    //表格删除事件
    handleDelete(index, row) {
      this.$confirm("确定删除" + row.uname + "这条信息吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios.delete("/user/deleteById/" + row.uid).then((respData) => {
            console.log(respData);
            if (respData.data.data) {
              this.$message.success("删除成功！");
              this.getByCondition();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //表格设置状态
    setStatus(index, row) {
      let status = 0;
      if (row.status == 0) {
        status = 1;
      }
      this.$axios
        .put("/user/modifyStatus/" + row.uid + "/" + status)
        .then((respData) => {
          console.log(respData);
          if (respData.data.data) {
            if (status == 1) {
              this.$message.success("启用成功！");
            } else {
              this.$message.success("禁用成功！");
            }
            this.getByCondition();
          }
        });
    },

    //数据渲染
    getStatus(status) {
      if (status == 0) {
        return "未租";
      }

      if (status == 1) {
        return "已租";
      }
    },
    statusClass(status) {
      switch (status) {
        case 0:
          return "tag1";
        case 1:
          return "tag2";
      }
    },

   
    //添加用户
    addUser() {
      this.$axios.post("/user/addUser", this.userModify).then((respData) => {
        if (respData.data.data) {
          this.$message.success("添加成功！");
          this.userModify = {};
          this.dialogFormVisible = false;
          this.getByCondition();
        }
      });
    },
    //修改用户
    modifyUser() {
      this.$axios.put("/user/modifyUser", this.userModify).then((respData) => {
        if (respData.data.data) {
          this.$message.success("修改成功！");
          this.userModify = {};
          this.dialogFormVisible = false;
          (this.disabled = false), this.getByCondition();
        }
      });
    },
  },
  mounted() {
    this.getCellList();
    this.getByCondition();
  },
};
</script>

<style scoped>
.tag1 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #fa556e;
  color: #fa5563;
}

.tag2 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #21bd88;
  color: #21bd88;
}
</style>
